// pages/feastPage/notOwner/notOwner.js
var netWork = require('../../../utils/netWork.js');
var api = require('../../../utils/api.js');
var apiCode = require('../../../utils/openApiCode.js');
const log = require('../../../utils/logs.js');
const QR = require('../../../utils/weapp-qrcode.js')
const regeneratorRuntime = require('../../../utils/runtime');
const {
  thirdAppId
} = require('../../../utils/api.js');
const app = getApp();
let latitude = ''; //纬度
let longitude = ''; //纬度
let accuracy = ''; //位置精度
let speed = ''; //速度
let positioningType = '';
let positioningStyle = '';
let updateFlag = false; //是否更新过用户信息
var isClick = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagesUrl: getApp().globalData.imagesUrl,
    topHeight: 0,
    ani: 0, //动画是否执行
    barcodeList: [],
    showAlert: false,
    barcode: '',
    alertContent: '',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    try {
      const {
        SDKVersion = ''
      } = wx.getSystemInfoSync();
      // 获取系统信息
      const that = this;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            addHeight: (res.screenHeight * res.pixelRatio - 1334) / 6
          });
        }
      });
      console.log(that.data.addHeight)
      log.info(SDKVersion)
      const compatibility = util.compareVersion(SDKVersion, '2.2.2');
      log.info(compatibility)
      if (compatibility == -1) {
        wx.showModal({
          content: '您的微信版本过低，请升级后再来吧~',
          confirmText: '我知道了',
          showCancel: false
        });
        return;
      }
    } catch (e) {}
    try {
      // 检测是否可以调用getUpdateManager检查更新
      if (!wx.canIUse("getUpdateManager")) return;
      let updateManager = wx.getUpdateManager();
      // 获取全局唯一的版本更新管理器，用于管理小程序更新
      updateManager.onCheckForUpdate(function (res) {
        log.info(res)
        // 监听向微信后台请求检查更新结果事件
        log.info("是否有新版本：" + res.hasUpdate);
        if (res.hasUpdate) {
          //如果有新版本
          // 小程序有新版本，会主动触发下载操作
          updateManager.onUpdateReady(function () {
            //当新版本下载完成，会进行回调
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，单击确定重启小程序',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启小程序
                  updateManager.applyUpdate();
                }
              }
            })
          })
          // 小程序有新版本，会主动触发下载操作（无需开发者触发）
          updateManager.onUpdateFailed(function () {
            //当新版本下载失败，会进行回调
            wx.showModal({
              title: '提示',
              content: '检查到有新版本，但下载失败，请稍后尝试',
              showCancel: false,
            })
          })
        }
      });
    } catch (e) {
      e
    }
    //高度处理
    let topHeight = wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight');
    this.setData({
      topHeight: topHeight,
      icon:  'common/loading',
    });
    api.judgeNetworkStatus(function (res) {
      if ("none" == res) return;
    });
    //查询基础数据
    this.queryInfo();
    this.queryBarcode()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  queryInfo() {
    const that = this;
    that.isLoading(true,'加载中...')
    netWork.POST_Json('banquet/info', {
      data: {
        qrcodeAPublish: wx.getStorageSync('qrCodeFeast'),
      },
      success: function (res) {
        that.isLoading(false)
        if (res.data.retCode == undefined) {
          wx.showToast({
            title: '操作失败，稍后重试',
            icon: 'none'
          })
        }
        if (res.data.retCode == '0000') {
          that.setData({
            feastInfo: res.data.data
          })

        } else {
          wx.showToast({
            title: '操作失败，稍后重试',
            icon: 'none'
          })
        }
      },
      fail: function (res) {
        that.isLoading(false)
        wx.showToast({
          title: '操作失败，稍后重试',
          icon: 'none'
        })
      }
    });
  },
  queryBarcode() {
    const that = this;
    that.isLoading(true,'加载中...')
    netWork.POST_Json('banquet/activeList', {
      data: {
        qrcodeAPublish: wx.getStorageSync('qrCodeFeast'),
      },
      success: function (res) {
        that.isLoading(false)
        if (res.data.retCode == undefined) {
          wx.showToast({
            title: '操作失败，稍后重试',
            icon: 'none'
          })
        }
        if (res.data.retCode == '0000') {
          that.setData({
            barcodeList: res.data.data
          })

        } else {
          wx.showToast({
            title: '操作失败，稍后重试',
            icon: 'none'
          })
        }
      },
      fail: function (res) {
        that.isLoading(false)
        wx.showToast({
          title: '操作失败，稍后重试',
          icon: 'none'
        })
      }
    });
  },
  ok() {
    this.setData({
      barcode: '',
      alertContent: '',
      showAlert: false
    })
  },

  isLoading(isLoading, loadingText) {
    this.setData({
      isLoading: isLoading,
      loadingText: loadingText
    })

  },
  back() {
    wx.navigateBack({
      delta: 1
    });
  },
  scanBarcode() {
    wx.scanCode({
      scanType: ['qrCode'],
      success: res => {
        if (res.result) {
          var str = decodeURIComponent(res.result)
           
          //请求后台，扫码激活
          const that = this;
          that.isLoading(true,'加载中...')
          netWork.POST_Json('banquet/activeBarcode', {
            data: {
              qrcodeAPublish: str,
              activityBanquetId: that.data.feastInfo.activityBanquetId
              // activityBanquetId:10
            },
            success: function (res) {
              that.isLoading(false,'加载中...')
              if (res.data.retCode == undefined) {
                that.setData({
                  showAlert: true, 
                  alertContent: '操作失败,请稍后重试'
                })
              }
              if (res.data.retCode == '0000') {
                wx.showToast({
                  title: '操作成功',
                  icon: 'none',
                  duration:3000
                });
                //
                that.queryBarcode();
                that.queryInfo();

              } else {
                that.setData({
                  showAlert: true,
                  barcode: "",
                  alertContent: res.data.retMsg
                })
              }
            },
            fail: function (res) {
              that.isLoading(false,'加载中...')
              that.setData({
                showAlert: true,
                barcode: "",
                alertContent: '操作失败,请稍后重试'
              })
            }
          });

        }

      }
    })
  },
  handlerGobackClick() {
    wx.navigateBack({
      delta: 1
    });}
})